<?php
/**
 * @description : the default page for 3.com
 * @author      : stephen.mo <stephen.mo@achievo.com>
 * @date        : Nov 9,2010
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set("display_errors","on");

//get parameters
$lang              = (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;
$telco_name        = (isset($_REQUEST['pg'])) ? $_REQUEST['pg'] : OAuthModelConfig::DEFAULT_GATEWAY_OF_H3G;
$call_back         = (isset($_REQUEST['call_back']) && !empty($_REQUEST['call_back'])) ? $_REQUEST['call_back'] : (isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : NULL);
$msisdn            = (isset($_REQUEST['msisdn']) && !empty($_REQUEST['msisdn'])) ? $_REQUEST['msisdn'] : NULL;
$network_type      = (isset($_REQUEST['nw']) && !empty($_REQUEST['nw'])) ? base64_decode($_REQUEST['nw']) : NULL;
$tvbauth           = (isset($_REQUEST['tvbauth'])) ? $_REQUEST['tvbauth'] : NULL;

//base path setting.
$base_server_path  = OAuthConfig::OAuthDomainUrl($_SERVER);
$back_to_url       = OAuthConfig::OAuthGetBackUrl($_SERVER);
$page_reset_url    = OAuthConfig::OAuthGetRestUrl($_SERVER);
//parse_str($call_back,$param_vars);
$failure_page      = $base_server_path."/$telco_name/".OAuthModelConfig::$telco_payment_fail;
$success_page      = $base_server_path."/$telco_name/".OAuthModelConfig::$telco_payment_succ;
$quick_login_url   = $base_server_path.OAuthConfig::DEFAULT_QUICKLY_LOGIN.'?pg='.$telco_name.'&redirect_to='.urlencode($call_back);
$plan_details_url  = $base_server_path.OAuthConfig::DEFAULT_SUPPORT_PLAN_DETAILS.'?lang='.$lang.'&pg='.$telco_name.'&tvbauth='.$tvbauth;
$error_url         = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL.'?act=request&flag=failure&pg='.$telco_name.'&lang='.$lang;
$message_url       = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;


//check the msisdn is valid.
$rec = OAuthUtils::decryptMsisdn($msisdn);
$msisdn_str  = (isset($rec['msisdn_str'])) ? $rec['msisdn_str'] : NULL;
$msisdn_time = (isset($rec['msisdn_time'])) ? $rec['msisdn_time'] : NULL;
if (empty($msisdn) || empty($msisdn_str) || ((time()-$msisdn_time)>OAuthModelConfig::GET_H3G_MSISDN_EXPIRE)) {
    $msgbox_url = $error_url.'&call_back='.urlencode($call_back).'&errids='.json_encode(array(1030,1035,1041));
    OAuthUtils::redirectTo($msgbox_url);
    exit;
}

//chk user is or not logined.
$rec_tvbauth = (!empty($tvbauth)) ? OAuthUtils::chkTvbAuth($tvbauth) : FALSE;
if (!$rec_tvbauth) {
    OAuthUtils::redirectTo($quick_login_url);
    exit;
}

$login_id = OAuthUtils::getLoginIdByTvbAuth($tvbauth);
$tvbauth_api = OAuthUtils::getTvbAuthByLoginId($login_id);
if (empty($login_id) || empty($tvbauth_api)) {
    $msgbox_url = $error_url.'&call_back='.urlencode($call_back).'&errids='.json_encode(array(1035,1036));
    OAuthUtils::redirectTo($msgbox_url);
    exit;
}

//if network type is WIFI the call a MSApi to buy this plan. 
//else redirect to h3g's shop address.
if ($network_type == OAuthModelConfig::OAUTH_USING_NETWORK_TYPE_WIFI) {
    $rev = OAuthUtils::buyH3GPlanForWifi($msisdn_str,$login_id);
    //bug plan with msisdn & login id.
    if (!$rev) {//fail
        $msgbox_url = $error_url.'&call_back='.urlencode($call_back).'&errids='.json_encode(array(1041));
        OAuthUtils::redirectTo($msgbox_url);
        exit;
    }
    else {//sucess.
        $call_back     = $call_back.'&ntype='.$network_type;
        $success_page .= '?oauth_callback='.urlencode($call_back).'&msisdn='.$msisdn.'&tvbauth='.$tvbauth_api;
        OAuthUtils::redirectTo($success_page);
        exit;
    }
}
else {//goto h3g's shop url and go on buy plan process.
    //need to check h3g user's subscribe from remote 3.com.hk
    $rec = OAuthUtils::h3gCheckByApi($msisdn_str,$login_id);
    if (empty($rec) || !$rec) {
        //if a not subscripted user' then goto third payment page and post data to third payment urls.
        $detail_vars = OAuthUtils::getPaymentConfDetail($telco_name);
        $h3g_shop_urls = $detail_vars['shop_url'];
        $mapping_lang = OAuthModelConfig::$mapping_third_parties_lang[$lang];
        if (empty($h3g_shop_urls)) {
            OAuthUtils::redirectTo($error_url.'&call_back='.urlencode($call_back).'&errids='.json_encode(array(1040)));
            exit;
        }
        $redirect_urls = $h3g_shop_urls.'?lang='.$mapping_lang; 
    }
    else {
        $redirect_urls = $success_page.'?lang='.$lang; 
    }
    
    //header url.
    $call_back = $call_back.'&ntype='.$network_type;
    $redirect_urls = $redirect_urls.'&oauth_callback='.urlencode($call_back).'&tvbauth='.$tvbauth_api.'&msisdn='.$msisdn;
    OAuthUtils::redirectTo($redirect_urls);
    exit;
}

?>
